package container_cmds

import (
	"encoding/json"
	"os"
	"path/filepath"
)

const _DEV_FILE = ".simple_dev.json"

type DevPkgVersion struct {
	Package    string `json:"package"`
	Version    string `json:"version"`
	PluginAddr string `json:"pluginAddr"`
}

type DevForwardPort struct {
	ContainerPort uint16 `json:"container_port"`
	HostPort      uint16 `json:"host_port"`
	Public        bool   `json:"public"`
}

type DevEnv struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type DevExtension struct {
	Id string `json:"id"`
}

type SimpleDevConfig struct {
	PkgVersionList  []DevPkgVersion  `json:"pkg_version_list"`
	ForwardPortList []DevForwardPort `json:"forward_port_list"`
	EnvList         []DevEnv         `json:"env_list"`
	ExtensionList   []DevExtension   `json:"extension_list"`
}

func LoadSimpleDevConfig(repoPath string) (*SimpleDevConfig, error) {
	path := filepath.Join(repoPath, _DEV_FILE)
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}
	cfg := &SimpleDevConfig{
		PkgVersionList:  []DevPkgVersion{},
		ForwardPortList: []DevForwardPort{},
		EnvList:         []DevEnv{},
		ExtensionList:   []DevExtension{},
	}
	err = json.Unmarshal(data, cfg)
	return cfg, err
}
